﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Galaktika.BI.Runtime
{
    using Galaktika.BI.Extensibility.Dom;
    using Galaktika.BI.Silverlight;

    public class VariablesContainer : IEnumerable<KeyValuePair<string, BrickVariable>>
    {
        SafeDictionary<string, BrickVariable> m_Cache;

        public VariablesContainer()
            : this(new BrickVariable[] { })
        {
        }

        public VariablesContainer(IEnumerable<BrickVariable> vars)
        {
            m_Cache = new SafeDictionary<string, BrickVariable>();
            foreach (BrickVariable var in vars)
            {
                m_Cache.Add(var.Name, var);
            }
        }

        public void Add(string name, BrickVariable var)
        {
            m_Cache.Add(name, var);
        }

        public bool IsExists(string varName)
        {
            return m_Cache.ContainsKey(varName);
        }

        public BrickVariable this[string varName]
        {
            get
            {
                return m_Cache[varName];
            }
            set
            {
                m_Cache[varName] = value;
            }
        }

        #region IEnumerable<KeyValuePair<string,BrickVariable>> Members

        public IEnumerator<KeyValuePair<string, BrickVariable>> GetEnumerator()
        {
            return m_Cache.GetEnumerator();
        }

        #endregion
        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((System.Collections.IEnumerable)m_Cache).GetEnumerator();
        }

        #endregion
    }

    [XmlRoot(Namespace = Galaktika.BI.Silverlight.Common.Namespace)]
    public class VariablesRoot
    {
        public VariablesRoot()
        {
        }

        public VariablesRoot(IEnumerable<BrickVariable> vars)
        {
            m_Vars.AddRange(vars);
        }

        private List<BrickVariable> m_Vars = new List<BrickVariable>();
        [XmlArray]
        [XmlArrayItem(typeof(BrickVariable))]
        public List<BrickVariable> Vars
        {
            get
            {
                return m_Vars;
            }
        }

        public static VariablesRoot FromXml(string xml)
        {
            return XmlUtility.XmlStr2Obj<VariablesRoot>(xml);
        }
    }

    [XmlRoot(Namespace = Galaktika.BI.Silverlight.Common.Namespace)]
    public class NameValueEntry
    {
        public NameValueEntry()
        {
        }

        public NameValueEntry(string name, string value)
        {
            this.Name = name;
            this.Value = value;
        }

        public string Name { get; set; }
        public string Value { get; set; }
    }

    [XmlRoot(Namespace = Galaktika.BI.Silverlight.Common.Namespace)]
    public class NameValueDictionary
    {
        public NameValueDictionary()
        {
        }

        public NameValueDictionary(IEnumerable<NameValueEntry> entries)
        {
            m_Entries.AddRange(entries);
        }

        [XmlIgnore]
        public string this[string name]
        {
            get
            {
                foreach (NameValueEntry entry in m_Entries)
                {
                    if (entry.Name == name)
                    {
                        return entry.Value;
                    }
                }

                return string.Empty;
            }
        }

        private List<NameValueEntry> m_Entries = new List<NameValueEntry>();
        [XmlArray]
        [XmlArrayItem(typeof(NameValueEntry))]
        public List<NameValueEntry> Entries
        {
            get
            {
                return m_Entries;
            }
        }

        public string ToXml()
        {
            return XmlUtility.Obj2XmlStr(this, Galaktika.BI.Silverlight.Common.Namespace);
        }

        public static NameValueDictionary FromXml(string xml)
        {
            return XmlUtility.XmlStr2Obj<NameValueDictionary>(xml);
        }
    }
}
